function formatObjToFormPostParams(data) {
    const paramsArray = [];
    for(key in data) {
        paramsArray.push(key + '=' + data[key]);
    }
    return paramsArray.join('&');
}

function formatString(value) {
  let formatStr = value.replace(/\\/g, "\\\\");// 先转义斜杠
  formatStr = formatStr.replace(/\"/g, "\\\"");// 再转义引号
  formatStr = encodeURIComponent(formatStr);
  return formatStr;
}

function processObject(obj) {
  const formattedObj = {};
  for (const key in obj) {
      if (obj.hasOwnProperty(key)) {
          const value = obj[key];
          if (Object.prototype.toString.call(value) == '[object Array]') {
            const result = value.map((item) => {
              if (Object.prototype.toString.call(item) == '[object Object]') {
                return processObject(item);
              } else if ( typeof item == 'string' ) {
                return formatString(item);
              } else {
                return item;
              }
            });
            formattedObj[encodeURIComponent(key)] = result;
          } else if (Object.prototype.toString.call(value) == '[object Object]') {
              formattedObj[encodeURIComponent(key)] = processObject(value);
          } else if (typeof value == 'string') {
              formattedObj[encodeURIComponent(key)] = formatString(value);
          } else {
            formattedObj[encodeURIComponent(key)] = value;
          }
      }
  }
  return formattedObj;
}

export function formatRequestDataToJSON(data) {
    let result;
    if (Object.prototype.toString.call(data) == '[object Array]') {
        result = [];
        for (let i = 0; i < data.length; i++) {
            const item = data[i];
            if (Object.prototype.toString.call(item) == '[object Object]') {
              result.push(processObject(item));
            } else {
              result.push(item);
            }
        }
    } else if (Object.prototype.toString.call(data) == '[object Object]') {
      result = processObject(data);
    } else {
      result = data;
    }
    return JSON.stringify(result);
}

export const formPostRequest = (url, data) => {
    const params = formatObjToFormPostParams(data);
    return window.axios.post(url, params, {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
      }
    })
};

// 加载扩展资源
export function loadExtendResource(extendResources) {
    console.log("加载extend资源: ", extendResources);
    if (!extendResources) {
        return;
    }
    // 加载css资源
    let pluginCssFiles = extendResources.pluginCssFiles;
    if (pluginCssFiles && pluginCssFiles.length > 0) {
        pluginCssFiles.forEach(path => {
            let link = document.createElement('link');
            link.setAttribute('rel', 'stylesheet');
            link.setAttribute('type', 'text/css');
            link.setAttribute('href', window._ctxPath + "/" + path + window._resSuffix);
            document.head.appendChild(link);
        });
    }
    // 加载js资源
    let pluginJsFiles = extendResources.pluginJsFiles;
    if (pluginJsFiles && pluginJsFiles.length > 0) {
        pluginJsFiles.forEach(path => {
            let script = document.createElement("script");
            script.src = window._ctxPath + "/" + path + window._resSuffix;
            document.body.appendChild(script);
        });
    }
}

